//
//  KFDReachability.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/9/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDReachability.h"
#import "client_config.h"

@interface KFDReachability ()

@property (strong,nonatomic) Reachability* internetReachability;
@property (strong,nonatomic) Reachability* wifiReachability;
@property (strong,nonatomic) Reachability* hostReachability;

@property (assign,nonatomic) NetworkStatus internetStatus;
@property (assign,nonatomic) NetworkStatus wifiStatus;
@property (assign,nonatomic) NetworkStatus hostStatus;

@end

@implementation KFDReachability

+(KFDReachability*)sharedReachability
{
#ifdef REACHABILITY_ENABLED
    static KFDReachability* reachability=nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        reachability=[[KFDReachability alloc] init];
    });
    return reachability;
#else
    return nil;
#endif
}

-(instancetype)init
{
    if (self=[super init]) {
        self.internetReachability=[Reachability reachabilityForInternetConnection];
        self.wifiReachability=[Reachability reachabilityForLocalWiFi];
        self.hostReachability=[Reachability reachabilityWithHostName:HOST];
        
        self.internetStatus=self.internetReachability.currentReachabilityStatus;
        self.wifiStatus=self.wifiReachability.currentReachabilityStatus;
        self.hostStatus=self.hostReachability.currentReachabilityStatus;
    }
    return self;
}

-(NetworkStatus)internetStatus
{
#ifdef REACHABILITY_ENABLED
    return _internetStatus;
#else
    return ReachableViaWiFi;
#endif
}

-(NetworkStatus)wifiStatus
{
#ifdef REACHABILITY_ENABLED
    return _wifiStatus;
#else
    return ReachableViaWiFi;
#endif
}

-(NetworkStatus)hostStatus
{
#ifdef REACHABILITY_ENABLED
    return _hostStatus;
#else
    return ReachableViaWiFi;
#endif
}

- (void) reachabilityChanged:(NSNotification *)note
{
#ifdef REACHABILITY_ENABLED
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    NetworkStatus status=curReach.currentReachabilityStatus;
    if (curReach==self.internetReachability) {
        self.internetStatus=status;
    }else if (curReach==self.wifiReachability){
        self.wifiStatus=status;
    }else if (curReach==self.hostReachability){
        self.hostStatus=status;
    }
    if (self.reachabilityChangedAction) {
        self.reachabilityChangedAction(self.internetStatus,self.wifiStatus,self.hostStatus);
    }
#endif
}

-(void)startNotifiers
{
#ifdef REACHABILITY_ENABLED
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    [self.internetReachability startNotifier];
    [self.wifiReachability startNotifier];
    [self.hostReachability startNotifier];
#endif
}

-(void)stopNotifiers
{
#ifdef REACHABILITY_ENABLED
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
    [self.internetReachability stopNotifier];
    [self.wifiReachability stopNotifier];
    [self.hostReachability stopNotifier];
#endif
}

@end
